package com.ykdl.member.kid.beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.reflect.TypeToken;
import com.ykdl.member.kid.models.ArticleBean;
import com.ykdl.member.kid.models.CatalogArticleItem;
import com.ykdl.member.kid.models.CatalogBean;
import com.ykdl.member.kid.models.CatalogItemBean;
import com.ykdl.member.kid.models.CatalogTopicItem;
import com.ykdl.member.kid.models.HomeBean;

import net.wxxr.http.interfaces.IJsonParser;

public class DoctorTalkChildListBean implements Serializable, IJsonParser {
	
	private static final long serialVersionUID = -7228951588952535753L;
	
	private ArrayList<Object>  objs = null;
	
	private int next_cursor = 0;
	private int previous_cursor = 0;
	private int total_count = 0;
	private boolean isExpired = false;
	public boolean isExpired() {
		return isExpired;
	}
	public void setExpired(boolean isExpired) {
		this.isExpired = isExpired;
	}
	public int getNext_cursor() {
		return next_cursor;
	}
	public void setNext_cursor(int next_cursor) {
		this.next_cursor = next_cursor;
	}
	public int getPrevious_cursor() {
		return previous_cursor;
	}
	public void setPrevious_cursor(int previous_cursor) {
		this.previous_cursor = previous_cursor;
	}
	public int getTotal_count() {
		return total_count;
	}
	public void setTotal_count(int total_count) {
		this.total_count = total_count;
	}
	public ArrayList<Object> getObjs() {
		if(objs == null){
			objs = new ArrayList<Object>();
		}
		
		return objs;
	}
	public void setObjs(ArrayList<Object> objs) {
		this.objs = objs;
	}

	@Override
	public IJsonParser parse(String json) throws Exception {
		Gson  gson = null;
		if(null != json){
			GsonBuilder b = new GsonBuilder();
			gson  = b.create();
			final DoctorTalkChildListBean  bean = new DoctorTalkChildListBean();
			if("null".equals(json))return bean;
			if("{}".equals(json) || "[]".equals(json))return bean;
			final JSONObject  object = new JSONObject(json);
			if(json.startsWith("{") && json.endsWith("}")
					&& object.has("error")
					&& object.getInt("error") == 20007){
				bean.setExpired(true);
				return bean;
			}
			
			final String  list = object.getString("list");
			if("null".equals(list) || list.equals("[]")) return bean;
			
			final JSONArray  doctor_talks_json_array = object.getJSONArray("list");
			if(doctor_talks_json_array != null && doctor_talks_json_array.length() > 0){
				for(int i = 0 ; i < doctor_talks_json_array.length() ; i++){
					final String doctor_talk_item = doctor_talks_json_array.getString(i);
					if(doctor_talk_item == null || doctor_talk_item.equals("{}")) continue;
					JSONObject  jo = new JSONObject(doctor_talk_item);
					if (jo.has("article_id") && !jo.isNull("article_id")){
						bean.getObjs().add(gson.fromJson(doctor_talk_item, ArticleBean.class));
					}else if(jo.has("catalog_id") && !jo.isNull("catalog_id")){
//						int catalog_type = jo.getInt("catalog_type");
						final String catalog_items = jo.getString("items");
						//if(catalog_items == null || catalog_items.equals("[]")) continue;
						List<CatalogItemBean>  item = null;
						//if(catalog_type == 1 || catalog_type == 3){ // 话题 或 问题
							item = gson.fromJson(catalog_items, new TypeToken<List<CatalogTopicItem>>(){}.getType());
//							if(null == ((CatalogTopicItem)item.get(0)).getItem()) continue;
						//}else if(jo.getInt("catalog_type") == 2){ //文章
						//	item = gson.fromJson(catalog_items, new TypeToken<List<CatalogArticleItem>>(){}.getType());
						//	if(null == ((CatalogArticleItem)item.get(0)).getItem()) continue;
						//}
						
						CatalogBean  catalog = gson.fromJson(doctor_talk_item, CatalogBean.class);
						
						if(item != null){
							catalog.setCatalogItems(item);
						}
						bean.getObjs().add(catalog);
					}
				}
			}
			bean.setPrevious_cursor(object.getInt("previous_cursor"));
			bean.setNext_cursor(object.getInt("next_cursor"));
			bean.setTotal_count(object.getInt("total_count"));
			
			return bean;
		}
		return null;
	}
	
	

}
